home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility2 / winfsr21.zip / WINFSR.C < prev    next >
Text File  |  1992-02-10  |  11KB  |  369 lines

  1.  
  2. //  WinFSR 2.1    by Fran Finnegan  (CompuServe: 76244,145)
  3. //  Copyright (c) 1991-92 Finnegan O'Malley & Company Inc.
  4. //        All Rights Reserved.
  5. //  First Published in PC Magazine, November 12, 1991.
  6.  
  7. #define NOCOMM
  8. #include <windows.h>
  9.  
  10. #include "winfsr.h"
  11.  
  12. typedef DWORD (FAR PASCAL *GETHEAPSPACES)(HANDLE hModule);
  13.  
  14. extern int     PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
  15. extern long  FAR PASCAL WndProc(HWND, WORD, WORD, DWORD);
  16. extern int   FAR PASCAL DlgProc(HWND, WORD, WORD, DWORD);
  17. extern int   FAR PASCAL DlgIcon(HWND, WORD, WORD, DWORD);
  18.  
  19. #define FALSE        0
  20. #define TRUE        1
  21.  
  22. #define ID_TIMER    1
  23.  
  24. static    GETHEAPSPACES    GetHeapSpaces;                                                    char *_="\x28\x63\x29 \x31\x39\x39\x31 \x46\x69\x6E\x6E\x65\x67\x61\x6E \x4F\x27\x4D\x61\x6C\x6C\x65\x79\x2E";
  25. static    WNDCLASS    mWc;
  26. static    HWND        mhWnd, mhDlgMain, mhDlgOptions;
  27. static    WORD        mwWarn = 20;    //  %
  28. static    int        miIcon = 1;    //  plain
  29. static    char        mszClassName [] = "WINFSR",
  30.             mszWindowName[64] = "System Resources",
  31.             mszWinIniIcon[] = "Icon",
  32.             mszWinIniName[] = "Name",
  33.             mszWinIniWarn[] = "%";
  34.  
  35. ////////////////////////////////////////////////////////////////////////
  36.  
  37. extern int     PASCAL WinMain(HANDLE ahInst, HANDLE ahPrevInst,
  38.                 LPSTR alpsCmdLine, int aiCmdShow)
  39. {
  40. auto    FARPROC     aFpDlgProc;
  41. auto    MSG        aMsg;
  42.  
  43. static    char        sszGetHeapSpaces[] = "GETHEAPSPACES";
  44.  
  45.             alpsCmdLine;  aiCmdShow;
  46.  
  47. //  retrieve GetHeapSpaces's address in the kernel
  48. if ((GetHeapSpaces = (GETHEAPSPACES)GetProcAddress(
  49.     GetModuleHandle("KERNEL"), sszGetHeapSpaces)) == NULL)
  50.     {
  51.     MessageBox(0, "Function not found!", sszGetHeapSpaces, 0);
  52.     return FALSE;
  53.     }
  54.  
  55. //  show any previous instance's dialog
  56. if (ahPrevInst)
  57.     {
  58.     mhWnd = FindWindow(mszClassName, NULL);
  59.     PostMessage(mhWnd, WM_QUERYOPEN, 0, 0L);
  60.     return FALSE;
  61.     }
  62.  
  63. //  get WIN.INI info
  64. miIcon = GetProfileInt(mszClassName, mszWinIniIcon, miIcon);
  65. GetProfileString(mszClassName, mszWinIniName, mszWindowName,
  66.     mszWindowName, sizeof(mszWindowName));
  67. mwWarn = GetProfileInt(mszClassName, mszWinIniWarn, mwWarn);
  68.  
  69. //  register the window class
  70. mWc.lpfnWndProc   = WndProc;
  71. mWc.hInstance      = ahInst;
  72. mWc.hIcon      = LoadIcon(ahInst, mszClassName);
  73. mWc.lpszClassName = mszClassName;
  74. RegisterClass(&mWc);
  75.  
  76. //  create and show the window as an icon
  77. mhWnd = CreateWindow(mszClassName, mszWindowName,
  78.     WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
  79.     CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, ahInst, NULL);
  80. ShowWindow(mhWnd, SW_SHOWMINIMIZED);
  81.  
  82. //  create and show the dialog
  83. aFpDlgProc = MakeProcInstance(DlgProc, ahInst);
  84. CreateDialog(ahInst, "DIALOG", mhWnd, aFpDlgProc);
  85.  
  86. //  update the icon and dialog every second
  87. SetTimer(mhWnd, ID_TIMER, 1000, NULL);
  88.  
  89. while (GetMessage(&aMsg, 0, WM_NULL, WM_NULL))
  90.     if (!(mhDlgMain && IsDialogMessage(mhDlgMain, &aMsg)))
  91.     {
  92.     TranslateMessage(&aMsg);
  93.     DispatchMessage(&aMsg);
  94.     }
  95.  
  96. FreeProcInstance(aFpDlgProc);
  97. return FALSE;
  98. }
  99.  
  100. ////////////////////////////////////////////////////////////////////////
  101.  
  102. extern long  FAR PASCAL WndProc(HWND ahWnd, WORD awMsg,
  103.                 WORD awParam, DWORD alParam)
  104. {
  105. auto    DWORD        adwKernel, adwGDI, adwUser;
  106. auto    WORD        awPctKernel, awPctGDI, awPctUser,
  107.             awPctFSR;
  108. auto    HDC        ahDc;
  109. auto    int        aiSmCyIcon;
  110. auto    PAINTSTRUCT    aPs;
  111. auto    char        aszText[256];
  112.  
  113. static    DWORD        sdwKernel = 1L;
  114. static    HANDLE        shModuleGDI, shModuleUser;
  115. static    WORD        swPctKernel, swPctGDI, swPctUser,
  116.             swPctFSR = 99;
  117. static    BOOL        sbWarned;
  118. static    int        siNumColors, siHide;
  119. static    RECT        sRt;
  120. static    char        *spsMode;
  121.  
  122. switch (awMsg)
  123.     {
  124.     case WM_CREATE:
  125.     switch ((WORD)GetWinFlags() & (WF_ENHANCED | WF_STANDARD |
  126.          WF_LARGEFRAME | WF_SMALLFRAME))
  127.         {
  128.         case WF_ENHANCED:     spsMode = "386 Enhanced";      break;
  129.         case WF_STANDARD:     spsMode = "Standard";          break;
  130.         case WF_LARGEFRAME:  spsMode = "Large-EMS Real";    break;
  131.         case WF_SMALLFRAME:  spsMode = "Small-EMS Real";    break;
  132.         default:         spsMode = "Real";              break;
  133.         }
  134.     shModuleGDI  = GetModuleHandle("GDI" );
  135.     shModuleUser = GetModuleHandle("USER");
  136.     siHide = GetProfileInt(mszClassName, "Hide", 9);
  137.     break;
  138.  
  139.     case WM_SIZE:
  140.     //  center the icon and text appropriately
  141.     *(DWORD *)&sRt.left   = 0L;
  142.     *(DWORD *)&sRt.right  = alParam;  //  same as GetClientRect
  143.     aiSmCyIcon = GetSystemMetrics(SM_CYICON);
  144.     InflateRect(&sRt, -((sRt.right    -
  145.         GetSystemMetrics(SM_CXICON)) / 2 + 1),
  146.         -((sRt.bottom - aiSmCyIcon)  / 2 + 1));
  147.     sRt.bottom = sRt.top  + aiSmCyIcon   / 2;
  148.     break;
  149.  
  150.     case WM_TIMER:
  151.     //  calculate the Free System Resources percentages
  152.     adwKernel = GetFreeSpace(0) / 1024;    //  1K granularity
  153.     adwGDI      = GetHeapSpaces(shModuleGDI );
  154.     adwUser   = GetHeapSpaces(shModuleUser);
  155.  
  156.     if (sdwKernel < adwKernel)  //    maximum global heap (in K)
  157.         sdwKernel = adwKernel;  //        that's been available
  158.     awPctKernel = (WORD)(10000L *         adwKernel    /
  159.                          sdwKernel );
  160.     awPctGDI    = (WORD)(10000L * LOWORD(adwGDI   ) /
  161.                       HIWORD(adwGDI   ));
  162.     awPctUser   = (WORD)(10000L * LOWORD(adwUser  ) /
  163.                       HIWORD(adwUser  ));
  164.  
  165.     if (swPctKernel  != awPctKernel   //  basis-point comparisons
  166.     ||  swPctGDI     != awPctGDI      //    instead of absolutes,
  167.     ||  swPctUser     != awPctUser  )  //    to prevent flickering
  168.         {
  169.         swPctKernel   = awPctKernel;
  170.         swPctGDI      = awPctGDI   ;
  171.         swPctUser      = awPctUser  ;
  172.         awPctFSR  = min(awPctGDI, awPctUser);   //    normal FSRs
  173.         if (awPctFSR  > awPctKernel)
  174.         awPctFSR  = awPctKernel;    //    including global heap
  175.         if (swPctFSR != awPctFSR / 100)   //  percent comparison
  176.         {
  177.         swPctFSR  = awPctFSR / 100;
  178.         InvalidateRect(ahWnd, &sRt, FALSE);
  179.         UpdateWindow(ahWnd);
  180.         }
  181.         wsprintf(aszText,
  182.             "%s-Mode Free Memory... %luK/%luK=%u%%\r\n"
  183.             "GDI... %u/%u=%u%%    USER... %u/%u=%u%%\r\n"
  184.             "Free System Resources... %u%%\r\n", (LPSTR)spsMode,
  185.                adwKernel ,          sdwKernel , awPctKernel / 100,
  186.             LOWORD(adwGDI   ), HIWORD(adwGDI   ), awPctGDI    / 100,
  187.             LOWORD(adwUser  ), HIWORD(adwUser  ), awPctUser   / 100,
  188.             swPctFSR);
  189.         SetDlgItemText(mhDlgMain, ID_TEXT, aszText);
  190.         if (mwWarn < swPctFSR)    //  high FSRs
  191.         sbWarned  = FALSE;
  192.         else            //   low FSRs
  193.         if (sbWarned == FALSE)
  194.             {
  195.             sbWarned  = TRUE ;        //  show the dialog
  196.             PostMessage(ahWnd, WM_QUERYOPEN, 0, 0L);
  197.             }
  198.         }
  199.     else
  200.         if (siHide-- == 0
  201.         &&    mhDlgOptions == 0)
  202.         ShowWindow(mhDlgMain, SW_HIDE); //  hide the dialog
  203.     break;
  204.  
  205.     case WM_PAINTICON:
  206.     if (ahDc = BeginPaint(ahWnd, &aPs))
  207.         {
  208.         DrawIcon(ahDc, sRt.left - 1, sRt.top - 1,
  209.             LoadIcon(mWc.hInstance, miIcon?  "PLAIN":  "CLASSIC"));
  210.         if (siNumColors == 0)
  211.         siNumColors  =    GetProfileInt(mszClassName, "Colors",
  212.                 GetDeviceCaps(ahDc, NUMCOLORS));
  213.         if (siNumColors >= 16)    //  check for colors (1.0: >= 8)
  214.         SetBkMode(ahDc, TRANSPARENT);
  215.         DrawText(ahDc,   aszText,    //  paint over icon
  216.             wsprintf(aszText, "%u%%", swPctFSR), &sRt,
  217.             DT_CENTER | DT_VCENTER | DT_SINGLELINE |
  218.             DT_NOCLIP | DT_NOPREFIX);
  219.         EndPaint(ahWnd, &aPs);
  220.         }
  221.     break;
  222.  
  223.     case WM_QUERYOPEN:
  224.     ShowWindow(mhDlgMain, SW_SHOW);     //  show the dialog
  225.     BringWindowToTop(mhDlgMain);
  226.     break;
  227.  
  228.     case WM_CLOSE:
  229.     KillTimer(ahWnd, ID_TIMER);
  230.     DestroyWindow(mhDlgMain);
  231.     mhDlgMain = 0;
  232.     DestroyWindow(ahWnd);
  233.     break;
  234.  
  235.     case WM_DESTROY:
  236.     PostQuitMessage(FALSE);
  237.     break;
  238.  
  239.     default:
  240.     return DefWindowProc(ahWnd, awMsg, awParam, alParam);
  241.     }
  242. return FALSE;
  243. }
  244.  
  245. ////////////////////////////////////////////////////////////////////////
  246.  
  247. extern int   FAR PASCAL DlgProc(HWND ahDlg, WORD awMsg,
  248.                 WORD awParam, DWORD alParam)
  249. {
  250. auto    RECT        aRt;
  251.  
  252.             alParam;
  253.  
  254. switch (awMsg)
  255.     {
  256.     case WM_INITDIALOG:
  257.     mhDlgMain = ahDlg;
  258.  
  259.     //  center dialog box
  260.     GetWindowRect(ahDlg, &aRt);
  261.     OffsetRect(&aRt, -aRt.left  , -aRt.top     );
  262.     MoveWindow(ahDlg,   //    correct way to center a dialog:
  263.         ((GetSystemMetrics(SM_CXSCREEN) - aRt.right ) / 2 + 4) & ~7,
  264.          (GetSystemMetrics(SM_CYSCREEN) - aRt.bottom) / 2,
  265.         aRt.right , aRt.bottom, FALSE);
  266.  
  267.     //  initialize controls
  268.     SetWindowText(ahDlg, mszWindowName);
  269.     SendMessage(mhWnd, WM_TIMER, 0, 0L);
  270.     return TRUE;    //  did process the message
  271.  
  272.     case WM_COMMAND:
  273.     if (awParam == ID_OPTIONS)
  274.         if (mhDlgOptions)
  275.         BringWindowToTop(mhDlgOptions);
  276.         else
  277.         {
  278.         auto    FARPROC     aFpDlgProc;
  279.  
  280.         aFpDlgProc = MakeProcInstance(DlgIcon, mWc.hInstance);
  281.         DialogBox(mWc.hInstance, "ICON", mhWnd, aFpDlgProc);
  282.         FreeProcInstance(aFpDlgProc);
  283.         SetFocus(GetDlgItem(ahDlg, ID_OPTIONS));
  284.         }
  285.     else
  286.         if (mhDlgOptions == 0)
  287.         ShowWindow(ahDlg, SW_HIDE);    //  hide the dialog
  288.     return TRUE;    //  did process the message
  289.     }
  290. return FALSE;    //  did not process the message
  291. }
  292.  
  293. ////////////////////////////////////////////////////////////////////////
  294.  
  295. extern int   FAR PASCAL DlgIcon(HWND ahDlg, WORD awMsg,
  296.                 WORD awParam, DWORD alParam)
  297. {
  298. auto    RECT        aRt;
  299. auto    char        aszText[64];
  300.  
  301. switch (awMsg)
  302.     {
  303.     case WM_INITDIALOG:
  304.     mhDlgOptions = ahDlg;
  305.  
  306.     //  overlay parent dialog box
  307.     GetWindowRect(mhDlgMain, &aRt);
  308.     MoveWindow(ahDlg, aRt.left  ,  aRt.top     ,
  309.               aRt.right  - aRt.left  ,
  310.               aRt.bottom - aRt.top     , FALSE);
  311.  
  312.     //  initialize controls
  313.     SetWindowText(ahDlg, mszWindowName);
  314.     CheckRadioButton(ahDlg, ID_ICON_CLASSIC, ID_ICON_PLAIN,
  315.                 ID_ICON_CLASSIC + miIcon);
  316.     SetDlgItemText(ahDlg, ID_NAME, mszWindowName);
  317.     GetProfileString(mszClassName, mszWinIniWarn, "20",
  318.         aszText, sizeof(aszText));
  319.     SetDlgItemText(ahDlg, ID_PERCENT, aszText);
  320.     return TRUE;    //  did process the message
  321.  
  322.     case WM_CTLCOLOR:
  323.     if (HIWORD(alParam) == CTLCOLOR_STATIC)
  324.         switch (GetDlgCtrlID(LOWORD(alParam)))
  325.         {
  326.         case ID_ICO_CLASSIC:
  327.         case ID_ICO_PLAIN  :
  328.             return (int)GetStockObject(NULL_BRUSH);
  329.         }
  330.     break;    //  did not process the message
  331.  
  332.     case WM_COMMAND:
  333.     switch (awParam)
  334.         {
  335.         case IDOK:
  336.         //  icon
  337.         miIcon = IsDlgButtonChecked(ahDlg, ID_ICON_PLAIN);
  338.         WriteProfileString(mszClassName, mszWinIniIcon,
  339.                          miIcon?  "1":  "0");
  340.         InvalidateRect(mhWnd, NULL, TRUE);
  341.  
  342.         //  name
  343.         GetDlgItemText(ahDlg, ID_NAME,      mszWindowName,
  344.                        sizeof(mszWindowName));
  345.         WriteProfileString(mszClassName,  mszWinIniName,
  346.                           mszWindowName);
  347.         SetWindowText(mhDlgMain, mszWindowName);
  348.         SetWindowText(mhWnd    , mszWindowName);
  349.  
  350.         //  %
  351.         GetDlgItemText(ahDlg, ID_PERCENT, aszText,
  352.                        sizeof(aszText));
  353.         WriteProfileString(mszClassName,  mszWinIniWarn,
  354.                           aszText);
  355.         mwWarn = GetProfileInt(mszClassName, mszWinIniWarn, mwWarn);
  356.  
  357.         PostMessage((HWND)-1, WM_WININICHANGE, 0, 0L);
  358.         //  fall through
  359.         case IDCANCEL:
  360.         mhDlgOptions = 0;
  361.         EndDialog(ahDlg, 0);
  362.         break;
  363.         }
  364.     return TRUE;    //  did process the message
  365.     }
  366. return FALSE;    //  did not process the message
  367. }
  368.  
  369.